<?php
namespace app\index\logic;
use think\Db;

class Recom
{
    /*推荐位数据获取*/
    public function getList()
    {

        $checkIpBlackResult = checkIpBlackByToken();
        if($checkIpBlackResult['status'] == true)
        {
            $where[] = ['type','=',2];
        }else{

            if(request()->scale_grade == 'C')
            {
                $where[] = ['type','=',2];
            }else{
                $where[] = ['type','=',1];
            }

        }

        $list = Db::table('quick_default_bookshelf')->where('agent_id','=',request()->agentId)
                ->where($where)->field('novel_id')->select();

        if(empty($list))
        {
            $list = Db::table('quick_default_bookshelf')->where('agent_id','=',0)
                ->where($where)->field('novel_id')->select();
        }

        $data = [];

        if($list)
        {

            $nids_arr = array_unique(array_column($list,'novel_id'));
            $where_v[] = ['id','in',$nids_arr];
            $where_v[] = ['in_shelf','=',1];

            $NovelModel = \think\facade\App::model('Novel', 'model');
            $novels = $NovelModel->getList($where_v,'id,origin_title,cover,brief,class_id,update_status',1,count($nids_arr));
            $novels = array_column($novels,null,'id');

            $cat_id_gh = array_unique(array_column($list, 'class_id'));
            $cats = Db::table('novel_class')->where('id','in',$cat_id_gh)->column('id,name');

            foreach ($nids_arr as $k=>$v)
            {
                if(!isset($novels[$v])){
                    continue;
                }
                $data[$k]['nid']            = $novels[$v]['id'];
                $data[$k]['bookname']       = $novels[$v]['origin_title'];
                $data[$k]['brief']          = $novels[$v]['brief'];
                $data[$k]['update_status']  = getByUpdateStatusName($novels[$v]['update_status']);
                $data[$k]['cat_name']       = isset($cats[$novels[$v]['class_id']]) ? $cats[$novels[$v]['class_id']] : '';
                $data[$k]['vertical_cover'] = strlen($novels[$v]['cover']) > 1 ? config('img_domain').$novels[$v]['cover'] : '';
            }
        }

        return $data;
    }

    /*
     * 首页推荐位
     * */
    public function getHome($params)
    {
        $recom = Db::table('quick_recommend')->where('agent_id','=',request()->agentId)
                ->field('id,title,en_title,freq,is_random')->order('id ASC')->select();
        if(empty($recom))
        {
            $recom = Db::table('quick_recommend')->where('agent_id','=',0)
                ->field('id,title,en_title,freq,is_random')->order('id ASC')->select();
        }

        $checkIpBlackResult = checkIpBlackByToken();
        if($checkIpBlackResult['status'] == true)
        {
            $where[] = ['type','=',2];
        }else{
            
            if(request()->scale_grade == 'C')
            {
                $where[] = ['type','=',2];
            }else{
                $where[] = ['type','=',1];
            }
        }

        $rids_gh = array_column($recom,'id');
        $where[] = ['rid','in',$rids_gh];
        $novels_r = Db::table('quick_recommend_novel')->where($where)->order('sort DESC,id DESC')
                ->field('rid,novel_id')->select();

        if($novels_r)
        {
            $nids_arr = array_unique(array_column($novels_r,'novel_id'));
            $where_v[] = ['id','in',$nids_arr];
            $where_v[] = ['in_shelf','=',1];

            $NovelModel = \think\facade\App::model('Novel', 'model');
            $novels = $NovelModel->getList($where_v,'id,origin_title,cover,brief,class_id,update_status',1,count($nids_arr));

            $cat_id_gh = array_unique(array_column($novels, 'class_id'));
            $cats = Db::table('novel_class')->where('id','in',$cat_id_gh)->column('id,name,en_name');

            $novels_new = [];
            foreach ($novels as $k=>$v)
            {
                $novels_new[$k]['nid']            = $v['id'];
                $novels_new[$k]['bookname']       = $v['origin_title'];
                $novels_new[$k]['brief']          = $v['brief'];
				
				$update_status = getByUpdateStatusName($v['update_status']);
				
				if($params['lang'] == 'en'){
					$novels_new[$k]['update_status'] = $update_status == '連載中' ? 'Serialize' : 'Completed';
					$novels_new[$k]['cat_name']       = isset($cats[$v['class_id']]) ? $cats[$v['class_id']]['en_name'] : '';
				}else{
					$novels_new[$k]['update_status']  = $update_status;
					$novels_new[$k]['cat_name']       = isset($cats[$v['class_id']]) ? $cats[$v['class_id']]['name'] : '';
				}

                $novels_new[$k]['vertical_cover'] = strlen($v['cover']) > 1 ? config('img_domain').$v['cover'] : '';
            }

            $novels_new = $novels_new ? array_column($novels_new,null,'nid') : [];
        }

        $recom_novels = [];
        foreach ($novels_r as $v)
        {
            if( isset($novels_new[$v['novel_id']]) )
            {
                $recom_novels[$v['rid']][] = $novels_new[$v['novel_id']];
            }
        }

        $recom_data = [];
        foreach ($recom as $k=>$v)
        {
            $recom_data[] = [
                'novels'    => $v['is_random'] == 1 ? $this->getRandBooks($recom_novels[$v['id']],6) : $recom_novels[$v['id']],
                'title'     => $params['lang'] == 'en' ? $v['en_title'] : $v['title'],
                'freq'      => $v['freq']
            ];
        }

        $recom_data_new = [];
        foreach ($recom_data as $v)
        {
            $recom_data_new[$v['freq']][] = $v;
        }

        $data_new['home'] = isset($recom_data_new[0]) ? $recom_data_new[0] : [];
        $data_new['male'] = isset($recom_data_new[1]) ? $recom_data_new[1] : [];
        $data_new['female'] = isset($recom_data_new[2]) ? $recom_data_new[2] : [];

        return $data_new;
    }

    /*
     * 获取随机书籍
     * */
    private function getRandBooks($novels,$randNum)
    {
        $random_keys = array_rand($novels,$randNum);
        $data = [];

        foreach ($random_keys as $key)
        {
            $data[] = $novels[$key];
        }

        return $data;
    }

}